package org.shoukaiseki.common.utils;

import org.apache.commons.io.FilenameUtils;
import org.apache.tika.mime.MimeType;
import org.apache.tika.mime.MimeTypes;
import org.shoukaiseki.common.exception.MessageVirtualException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.MimeTypeUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 *
 **/
public class FileHttpUtils {

    public static final Logger logger= LoggerFactory.getLogger(FileHttpUtils.class);

    /**
     * 读取文件
     * @param url
     * @param response
     */
    public static void view(String url, HttpServletResponse response) {

        File file = new File(url);
        if (!file.exists()) {
            logger.error("fileUrl={}",url);
            throw new MessageVirtualException("文件不存在");
        }

        try (FileInputStream is = new FileInputStream(file);
             BufferedInputStream bis = new BufferedInputStream(is)) {

            OutputStream out = response.getOutputStream();
            byte[] buf = new byte[1024];
            int bytesRead;

            while ((bytesRead = bis.read(buf)) > 0) {
                out.write(buf, 0, bytesRead);
            }
        } catch (IOException e) {
            logger.error(e.toString());
            throw new MessageVirtualException("读取/下载文件出错");
        }
    }



    public static  String formatContentType(String fileName){
        MimeTypes allTypes = MimeTypes.getDefaultMimeTypes();
        MimeType mimeType = allTypes.getMimeType(fileName);

        return mimeType.getType().toString();
    }



    public static final File getAbsoluteFile(String uploadDir, String fileName)
    {
        return  getAbsoluteFile(uploadDir + "/" + fileName);
    }

    public static final File getAbsoluteFile(String absolutePath)
    {
        File desc = new File(absolutePath);

        if (!desc.getParentFile().exists())
        {
            desc.getParentFile().mkdirs();
        }
        if (!desc.exists())
        {
            try {
                desc.createNewFile();
            } catch (IOException e) {
                throw new MessageVirtualException("",e);
            }
        }
        return desc;
    }

    /**
     * 格式化文件名,文件名最大长度默认为60
     * @param fileName   文件名
     * @return
     */
    public static final String formatFileName(String fileName) {
        return formatFileName(fileName, 60);
    }

    /**
     * 格式化文件名,文件名最大长度默认为60
     * @param fileName           文件名
     * @param fileNameMaxLength  60
     * @return
     */
    public static final String formatFileName(String fileName, int fileNameMaxLength){
        String fileNameTmp=fileName;
        fileNameTmp=fileNameTmp.replaceAll("\\\\","/");
        if(fileNameTmp.indexOf("/")>-1){
            if(fileNameTmp.lastIndexOf("/")+1<fileNameTmp.length()){
                fileNameTmp=fileNameTmp.substring(fileNameTmp.lastIndexOf("/")+1);
            }
        }
        if(fileName.contains(".")){
            fileNameTmp=fileName.substring(0,fileName.lastIndexOf("."));
        }
        if (fileNameTmp.length()>fileNameMaxLength) {
            fileNameTmp=fileNameTmp.substring(fileNameTmp.length()-fileNameMaxLength);
        }
        return fileNameTmp;
    }
}
